﻿using System.Windows.Forms;
using Fiddler;
using TrackingAssist.WebTrends;

[assembly: Fiddler.RequiredVersion("2.2.4.0")]
public class TrackingAssistTool : IAutoTamper
{
    private TabPage oPage;
    private WebTrendsView webTrendsView;

    public void OnBeforeUnload() { }

    // FiddlerExtension Interface, to add atlasview to fiddler
    public void OnLoad()
    {
        ResourceManager.GetIconList();

        this.oPage = new TabPage("WebTrends Tracking");
        FiddlerApplication.UI.imglSessionIcons.Images.Add(ResourceManager.GetImage(AppIcons.AppIcon));
        oPage.ImageIndex = FiddlerApplication.UI.imglSessionIcons.Images.Count - 1;
        this.webTrendsView = new WebTrendsView();
        this.oPage.Controls.Add(webTrendsView);
        this.webTrendsView.Dock = DockStyle.Fill;
        FiddlerApplication.UI.tabsViews.TabPages.Add(this.oPage);
    }

    // Called before the user can edit a request using the Fiddler Inspectors
    public void AutoTamperRequestBefore(Session oSession)
    {
        if (WebTrendsView.EnableAtlasAssist)
        {
            WebTrends webtrends;
            if (WebTrends.TryParse(oSession.url, out webtrends))
            {
                webTrendsView.AddCapturedWebTrends(webtrends);
            }

        }
    }

    // Called after the user has had the chance to edit the request using the Fiddler Inspectors, but before the request is sent
    public void AutoTamperRequestAfter(Session oSession) { }

    // Called before the user can edit a response using the Fiddler Inspectors, unless streaming.
    public void AutoTamperResponseBefore(Session oSession) { }

    // Called after the user edited a response using the Fiddler Inspectors.  Not called when streaming.
    public void AutoTamperResponseAfter(Session oSession) { }

    // Called Fiddler returns a self-generated HTTP error (for instance DNS lookup failed, etc)
    public void OnBeforeReturningError(Session oSession) { }
}

